﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Xml.Linq;
using System.Web.UI.WebControls;
using Wowhead.Common;

namespace Wowhead.WebControls
{
    public class ModelViewer : BaseModelViewer
    {
        public ModelViewer()
        {
            this.Width = 290;
            this.Height = 400;
            this.Race = Race.BloodElf;
            this.Gender = Gender.Female;
        }

        public object DataSource { get; set; }
        public string DataSlotField { get; set; }
        public string DataIDField { get; set; }

        public Race Race { get; set; }
        public Gender Gender { get; set; }

        private Literal container;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            foreach (Control ctl in this.Controls)
                ctl.RenderControl(writer);
        }

        public override void DataBind()
        {
            base.DataBind();
            object dataSource = DataSource;
            List<Pair<int, int>> pairs = new List<Pair<int, int>>();

            string slotField = (string.IsNullOrEmpty(this.DataSlotField) ? "Slot" : this.DataSlotField);
            string idField = (string.IsNullOrEmpty(this.DataIDField) ? "ID" : this.DataIDField);
            
            string race = this.Race.ToValueString();
            string gender = this.Gender.ToValueString();

            if (dataSource is IEnumerable)
            {
                foreach (object obj in (IEnumerable)dataSource)
                {
                    Pair<int, int> pair = new Pair<int, int>();
                    pair.Value1 = int.Parse(DataBinder.GetPropertyValue(obj, slotField).ToString());
                    pair.Value2 = int.Parse(DataBinder.GetPropertyValue(obj, idField).ToString());
                    pairs.Add(pair);
                }
            }
            else
            {
                Pair<int, int> pair = new Pair<int, int>();
                pair.Value1 = int.Parse(DataBinder.GetPropertyValue(dataSource, slotField).ToString());
                pair.Value2 = int.Parse(DataBinder.GetPropertyValue(dataSource, idField).ToString());
                pairs.Add(pair);           
            }

            string itemlist = string.Join(",", (from item in pairs select (item.Value1.ToString() + "," + item.Value2.ToString())).ToArray());

            XElement control = new XElement("object",
                                    new XAttribute("data", "http://static.wowhead.com/modelviewer/ModelView.swf"),
                                    new XAttribute("width", this.Width),
                                    new XAttribute("height", this.Height),
                                    new XAttribute("type", "application/x-shockwave-flash"),
                                    new XElement("param",
                                        new XAttribute("value", "high"),
                                        new XAttribute("name", "quality")),
                                    (this.BackColor.HasValue ?
                                        new XElement("param",
                                            new XAttribute("name", "bgcolor"),
                                            new XAttribute("value", System.Drawing.ColorTranslator.ToHtml(this.BackColor.Value)))
                                            : 
                                        new XElement("param",
                                            new XAttribute("value", "transparent"),
                                            new XAttribute("name", "wmode"))),
                                    new XElement("param",
                                        new XAttribute("value", "always"),
                                        new XAttribute("name", "allowsscriptaccess")),
                                    new XElement("param",
                                        new XAttribute("value", "false"),
                                        new XAttribute("name", "menu")),
                                    new XElement("param",
                                        new XAttribute("value", string.Format("model={1}{2}&modelType=16&contentPath=http://static.wowhead.com/modelviewer/&blur=1&equipList={0}", itemlist, race, gender)),
                                        new XAttribute("name", "flashvars")),
                                    new XElement("param",
                                        new XAttribute("movie", "http://static.wowhead.com/modelviewer/modelviewer_scale.swf?4")));
            container = new Literal();
            container.ID = "container";


            container.Text = control.ToString();
            this.Controls.Add(container);
        }
    }
}
